package com.qianfeng.dbsm.impl;

import com.alibaba.fastjson.JSONObject;
import com.qianfeng.dbsm.common.ExpressEnum;
import com.qianfeng.dbsm.entity.TbOrderLogistics;
import com.qianfeng.dbsm.express.KdApiExpressSearchDemo;
import com.qianfeng.dbsm.service.ExpressService;
import com.qianfeng.dbsm.service.TbOrderLogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExpressServiceImpl implements ExpressService {

    @Autowired
    TbOrderLogisticsService logisticsService;

    /**
     * 通过快递单号 和 快递公司编码查询 快递信息
     * @param expressNumber
     * @param expressCode
     * @return
     */
    @Override
    public Object queryExpressByExpress(String expressNumber, String expressCode,String customerName) {

       try{
           KdApiExpressSearchDemo kdApiExpressSearchDemo = new KdApiExpressSearchDemo();
           String shipperCode = expressCode;//"YTO";
           String logisticCode = expressNumber;// "YT6741087632364";
           String result = kdApiExpressSearchDemo.orderOnlineByJson(shipperCode,logisticCode,customerName);
           Object parse = JSONObject.parse(result);
           return parse;
       }catch (Exception e){
           throw new RuntimeException("查询失败");
       }
    }

    /**
     * 通过快递单号查询 当前快递单号的物流跟踪
     * @param orderNumber
     * @return
     */
    @Override
    public Object queryExpressByOrderNumber(String orderNumber) {

        List<TbOrderLogistics> orderLogisticsList = logisticsService.queryByOrderNumber(orderNumber);
        if(orderLogisticsList != null && orderLogisticsList.size() > 0){
            TbOrderLogistics logistics = orderLogisticsList.get(0);
            logistics.getExpressNo();
            KdApiExpressSearchDemo kdApiExpressSearchDemo = new KdApiExpressSearchDemo();
            try{
                String result = kdApiExpressSearchDemo.orderOnlineByJson(logistics.getLogisticsType(),logistics.getExpressNo(),logistics.getReceiverName());
                Object parse = JSONObject.parse(result);
                return parse;
            }catch (Exception e){
                throw new RuntimeException("查询失败!!!");
            }
        }else{
            return "当前订单暂未发货";
        }
    }
}
